package a.student.dao;

import java.util.ArrayList;

import a.student.library.*;
import a.student.helper.DatabaseHelper;
import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;

public class CourseDAO extends BaseDao<Course>{

	public CourseDAO(Context context, String databaseName) {
		super(context, databaseName);
	}
	
	public CourseDAO(SQLiteDatabase db) {
		super(db);
	}
	
	public CourseDAO(DatabaseHelper helper) {
		super(helper);
	}
	
	public long addNewCourse(Course courseInfo){

		ContentValues cv=new ContentValues();
		cv.put(Course.COURSE_CNAME, courseInfo.courseName);
		cv.put(Course.COURSE_SID, courseInfo.semesterID);
		cv.put(Course.COURSE_DATESTART, courseInfo.dateStart);
		cv.put(Course.COURSE_DATESEND, courseInfo.dateEnd);
		long result = super.insert(Course.COURSE_TBLNAME, cv);
		if(result > 0)
			courseInfo.setCourseId((int)result);
		return result;
		
	}

	public long removeCourse(Course courseInfo){
		
		ContentValues cv = new ContentValues();
		cv.put(Course.COURSE_ID, courseInfo.getCourseId());
		long count = super.delete(Course.COURSE_TBLNAME, cv);
		return count;
		
	}
    
    public ArrayList<Course> getAllCourse(){
    	Cursor c = super.getAll(Course.COURSE_TBLNAME, Course.COURSE_DEFAULT_SORT_ORDER);
    	ArrayList<Course> array = new ArrayList<Course>();
    	if(c==null)
    		return array;
    	while(!c.isAfterLast()){
    		array.add(new Course(c));
    		c.moveToNext();
    	}
    	c.close();
    	return array;
    }
    
    
    
    public Course findCourse(final int classID) {
        ContentValues cv = new ContentValues();
        cv.put(Course.COURSE_ID, classID);
        Cursor c = super.find(Course.COURSE_TBLNAME, cv,Course.COURSE_DEFAULT_SORT_ORDER);
        Course course = null;
        if (c != null) {
            try {
            	course = new Course(c);
            } catch (Exception e) {
            	e.printStackTrace();
			}
            finally {
                c.close();
            }
        }
        return course;
    }
    
	public boolean updateCourseDetail(Course courseInfo){
		
		boolean flag = true;
		ContentValues cv=new ContentValues();
		cv.put("C_Name", courseInfo.courseName);
		cv.put("Date_Start", courseInfo.dateStart);
		cv.put("Date_End", courseInfo.dateEnd);
		flag = super.update(Course.COURSE_TBLNAME, cv, Course.COURSE_ID + "=?", new String[]{String.valueOf(courseInfo.getCourseId())})>0;
    	return flag;
	}


}
